/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import pojo.Activities;
import java.sql.Connection;
import java.sql.PreparedStatement;
//import java.sql.ResultSet;
import java.sql.ResultSet;
import java.sql.SQLException;
import model.LogFile;

/**
 *
 * @author A7med Kotb
 */
public class ActivitiesDao
{
   Connection con;
    public ActivitiesDao(Connection connection)
    {
       
            
            con =connection;
    }
/////////////////////////////////////////Adding new Activity///////////////////////////////    
    public void add(Activities act)
    {
        try
        {
            PreparedStatement pstmt = con.prepareStatement("insert into activities values(?,?)") ;
            pstmt.setInt(1,act.getAId());
            pstmt.setString(2,act.getDescription());
            pstmt.execute() ;
        }
        catch(SQLException ex1)
        {
            ex1.printStackTrace();
        }
    }
//////////////////////////////////////delete Activity/////////////////////////////////////////
    public void delete(Activities act)
    {
        try
        {
            PreparedStatement pstmt = con.prepareStatement("delete from activities where a_id=?") ;
            pstmt.setInt(1,act.getAId());
            pstmt.execute() ;
        }
        catch(SQLException ex1)
        {
            ex1.printStackTrace();
        }
    }
//////////////////////////////////////update Activity//////////////////////////////////////////////////
    public void update(Activities act)
    {
        try 
        {
            boolean first=false;
            String updatetxt="";
            if(act.getDescription()!=null)
            {   first=true;
                updatetxt="description='"+act.getDescription()+"'";
            }
            PreparedStatement ps=con.prepareStatement("update activities set "+updatetxt+" where a_id=?");
            ps.setInt(1, act.getAId());
            ps.executeUpdate();
            
        } 
        catch (SQLException ex) 
        {
            ex.printStackTrace();
        }
    
    }
    
    //get All Activity data by its name
    public Activities getByName(String name)
    {
        Activities act=new Activities();
        try
        {
            PreparedStatement pstmt = con.prepareStatement("select * from activities where name=?") ;
            pstmt.setString(1,name);
            ResultSet rset=pstmt.executeQuery();
            if(rset.next())
            {
                act.setAId(rset.getInt(1));
                act.setDescription(rset.getString(2));
                act.setName(rset.getString(3));
                LogFile.log(" System ", " successfully retreived data for activity with name : ["+name+"] from database");
                return act;
            }
            else{
                LogFile.log(" System ", " failed to retreive data for the activity : ["+name+"] from database , may be there is no activity with that Name .");
                return null;
            }
        }
        catch(SQLException ex1)
        {
            ex1.printStackTrace();
            LogFile.log(" System ", " encountered the following exception while trying to retreive data for activity with name : ["+name+"] , the problem is :["+ex1+"]");
            return null;
        }
    }
    
    public Activities getById(Integer id)
    {
        Activities act=new Activities();
        try
        {
            PreparedStatement pstmt = con.prepareStatement("select * from activities where a_id=?") ;
            pstmt.setInt(1,id);
            ResultSet rset=pstmt.executeQuery();
            rset.next();
            act.setAId(rset.getInt(1));
            act.setDescription(rset.getString(2));
            act.setName(rset.getString(3));   
        }
        catch(SQLException ex1)
        {
            ex1.printStackTrace();
        }
         return act;
    }
    
}
